import time


class A(object):
    pass


# str
"""
class People:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def __str__(self):
        return '<name:%s age%s>'%(self.name,self.age)
    def run(self):
        return self.name
obj = People('长帅',18)
print(obj)
print(obj.run())
"""
# del
"""
class Foo:
    def __init__(self,x,filepath,encoding='utf8'):
        self.x=x
        self.f=open(filepath,'rt',encoding=encoding)
    def __del__(self):
    # 在对象被删除时先自动触发该方法,
    # 可以用来回收对象以外其他相关资源,比如系统资源
        print('__del__正在运行')
        self.f.close()
obj = Foo(1,'a.txt')
print(obj.f.read()) # self.f.read()
# obj.f.close()
# print('不是对象生成运行__del__，对象正在使用的时候不会运行')
time.sleep(3)
# 对象使用不会运行__del__
print(obj.f.read())
"""

# call
# __call__: 在对象被调用时会自动触发该方法
"""
class Foo:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def __call__(self, *args, **kwargs):
        # args : 不定长传参，返回值是元组 (1,2,8,8,8)
        # **kwargs ：不定长关键字，返回值是字典 {'x': 3, 'y': 4,"姓名":"王长帅"}
        print(self,args,kwargs)
    def run(self):
        print('run')

obj1 = Foo(1,2)
# obj1.run()
# 没写__call__不可以调用
# obj1()
# 写了__call__可以调用
# obj1() # <__main__.Foo object at 0x0196F570> () {}
obj1(1,2,8,8,8,x=3,y=4,姓名="王长帅") # obj1的地址值  <__main__.Foo object at 0x0181F570>
# # 省略了
# obj1.__call__(1,2,x=3,y=4)
"""
class Foo(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def __call__(self, sex,classid):

        print(self,sex,classid)
    def run(self):
        print('run')

obj1 = Foo("王长帅",99)
print(obj1.name)
print(obj1.age)
obj1("男","1702") # 调用__call__()方法 <__main__.Foo object at 0x0142EC90> 男 1702


